﻿using Microsoft.AspNetCore.Mvc;
using SYSoft.Model;
using SYSoft.Model.Models.DeviceManage;
using SYSoft.Services.DeviceManage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SYSoft.Controllers.DeviceManage
{
    public class DeviceAlarmController : BaseController
    {
        IDeviceAlarmServices deviceAlarmServices;
        public DeviceAlarmController(IDeviceAlarmServices deviceAlarmServices)
        {
            this.deviceAlarmServices = deviceAlarmServices;
        }



        /// <summary>
        /// 获取当日设备报警信息
        /// </summary>
        /// <param name="deviceid"></param>
        /// <param name="starttime"></param>
        /// <param name="stoptime"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel> GetDayliyAlarmInfo(int deviceid)
        {
            DateTime dt1 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
            DateTime dt2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59");
            List<DeviceAlarmInfo> list = null;
            if (deviceid == 8888)
                list = await deviceAlarmServices.Query(d => d.AlarmStatus == 0 && d.CreatTime > dt1 && d.CreatTime < dt2);
            else
                list = await deviceAlarmServices.Query(d => d.DeviceId == deviceid && d.AlarmStatus == 0 && d.CreatTime > dt1 && d.CreatTime < dt2);
            if (list != null && list.Count > 0)
            {
                return MessageModel.CreateSuccessResult(list);
            }
            return new MessageModel("未查到数据");
        }


        ///// <summary>
        ///// 获取设备报警信息
        ///// </summary>
        ///// <param name="deviceid"></param>
        ///// <param name="starttime"></param>
        ///// <param name="stoptime"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public async Task<MessageModel<PageModel<DeviceAlarmInfo>>> GetAlarmInfo(int deviceid, DateTime starttime, DateTime stoptime)
        //{
        //    var list = await deviceAlarmServices.Query(d => d.DeviceId == deviceid && d.CreateTime > starttime && d.CreateTime < stoptime);
        //}


        /// <summary>
        /// 分页多条件查询报警信息
        /// </summary>
        /// <param name="deviceid"></param>
        /// <param name="starttime"></param>
        /// <param name="stoptime"></param>
        /// <param name="alarmdegree"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<PageModel<DeviceAlarmInfo>>> PageDeviceAlarmInfo(int deviceid, DateTime starttime, DateTime stoptime, int alarmdegree, int page, int size)
        {
            var model = await deviceAlarmServices.conditionsSelect(deviceid, starttime, stoptime, alarmdegree, page, size);
            if (model.data != null && model.data.Count > 0)
            {
                return MessageModel.CreateSuccessResult(model);
            }
            return MessageModel.CreateFailedResult<PageModel<DeviceAlarmInfo>>("");
            
        }

        /// <summary>
        /// 多条件查询报警信息 
        /// </summary>
        /// <param name="deviceid"></param>
        /// <param name="starttime"></param>
        /// <param name="stoptime"></param>
        /// <param name="alarmdegree"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<List<DeviceAlarmInfo>>> ListDeviceAlarmInfo(int deviceid, DateTime starttime, DateTime stoptime, int alarmdegree)
        {
            var model = await deviceAlarmServices.listConditionsSelect(deviceid, starttime, stoptime, alarmdegree);
            return MessageModel.CreateSuccessResult(model);
        }


        /// <summary>
        /// 设备报警类别信息分布统计 
        /// </summary>
        /// <param name="deviceid"></param>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
        /// <returns>返回前端图表所需的字典</returns>
        [HttpPost]
        public async Task<MessageModel<List<Dictionary<string, object>>>> AlarmDistribution(int deviceid, DateTime starttime, DateTime endtime)
        {
            var model = await deviceAlarmServices.Query(d => d.DeviceId == deviceid && d.CreatTime > starttime && d.CreatTime < endtime);
            if (model!=null && model.Count>0)
            {
               var list = deviceAlarmServices.alarmDistribution(model);
                return MessageModel.CreateSuccessResult(list);
            }
            return MessageModel.CreateFailedResult<List<Dictionary<string, object>>>("");
        }
    }
}
